package com.zhf.client;

import com.zhf.model.*;

import java.io.IOException;
import java.util.List;

public class ClientManagerView {

    ClientTrainUI clientTrainUI = new ClientTrainUI();
    ClientExamUI clientExamUI = new ClientExamUI();
    private ClientInitClose cic;
    public ClientManagerView(){
    }

    public ClientManagerView(ClientInitClose cic) {
        this.cic = cic;
    }

    public void managerMainView(){
        System.out.println("\n------------请选择你要进行的操作------------");
        System.out.println("1.学员管理");
        System.out.println("2.考题管理");
        System.out.println("3.退出");
    }

    public void TraineeManageView(){
        System.out.println("****************************");
        System.out.println("1. 增加学员");
        System.out.println("2. 删除学员");
        System.out.println("3. 修改学员");
        System.out.println("4. 查找学员");
        System.out.println("5. 遍历学员");
        System.out.println("6. 返回上一级菜单");
    }

    public void ExamQuestionsView() {
        System.out.println("****************************");
        System.out.println("1. 增加考题");
        System.out.println("2. 删除考题");
        System.out.println("3. 修改考题");
        System.out.println("4. 查找考题");
        System.out.println("5. 导入考题");
        System.out.println("6. 返回上一级菜单");
    }

    public void managerMainOperate() throws IOException, ClassNotFoundException {
        while (true){
            managerMainView();
            int choose = ClientScanner.getScanner().nextInt();
            switch (choose){
                case 1:
                    clientTrainUI: while (true){
                        TraineeManageView();
                        int choose1 = ClientScanner.getScanner().nextInt();
                        switch(choose1){
                            case 1:
                                // 接收用户端输入并将信息传给服务端进行校验
                                UserMessage userMessage = clientTrainUI.addTrainee();
                                // 先通过发送路由给服务器，让服务器知道要调用哪个接口
                                cic.getOos().writeObject(ApiRoute.ADD_TRAINEE);
                                cic.getOos().writeObject(userMessage);
                                System.out.println("客户端发送新增学员账户信息成功！");
                                UserMessage userMessageResponse = (UserMessage)cic.getOis().readObject();
                                if("success".equals(userMessageResponse.getType())){
                                    System.out.println("添加学员信息成功！");
                                }
                                else{
                                    System.out.println("添加学员信息失败！");
                                }
                                break;
                            case 2:
                                User user = clientTrainUI.deleTrainee();
                                cic.getOos().writeObject(ApiRoute.DELETE_TRAINEE);
                                cic.getOos().writeObject(user);
                                System.out.println("客户端发送删除学员账户信息成功！");
                                UserMessage userMessage1 = (UserMessage) cic.getOis().readObject();
                                if("success".equals(userMessage1.getType())){
                                    System.out.println("删除学员信息成功！");
                                }
                                else{
                                    System.out.println("删除学员信息失败！");
                                }
                                break;
                            case 3:
                                UserUpdVo userUpdVo = clientTrainUI.updateTrainee();
                                cic.getOos().writeObject(ApiRoute.UPDATE_TRAINEE);
                                cic.getOos().writeObject(userUpdVo);
                                System.out.println("客户端发送修改学员账户信息成功！");
                                UserUpdVo userUpdVo1 = (UserUpdVo) cic.getOis().readObject();
                                if("success".equals(userUpdVo1.getUserMessage().getType())){
                                    System.out.println("修改学员信息成功！");
                                }
                                else{
                                    System.out.println("修改学员信息失败！");
                                }
                                break;
                            case 4:
                                User user1 = clientTrainUI.selectTrainee();
                                cic.getOos().writeObject(ApiRoute.SELECT_TRAINEE);
                                cic.getOos().writeObject(user1);
                                System.out.println("客户端发送查找学员账户信息成功！");
                                UserMessage userMessageResponse1 = (UserMessage)cic.getOis().readObject();
                                if("success".equals(userMessageResponse1.getType())){
                                    System.out.println("查找学员信息成功！");
                                    System.out.println("该学员信息为：" + userMessageResponse1.getUser());
                                }
                                else{
                                    System.out.println("查找学员信息失败！");
                                }
                                break;
                            case 5:
                                cic.getOos().writeObject(ApiRoute.ITERATOR_TRAINEE);
                                System.out.println("服务器断返回的学员列表信息为：");
                                List<User> userList = (List<User>) cic.getOis().readObject();
                                if(userList != null){
                                    for (User u: userList) {
                                        System.out.println(u);
                                    }
                                }
                                break;
                            case 6:
                                break clientTrainUI;
                            default:
                                System.out.println("输入错误，请重新选择！");
                        }
                    }
                    break;
                case 2:
                    clientExamUI: while(true){
                        ExamQuestionsView();
                        int choose2 = ClientScanner.getScanner().nextInt();
                        switch (choose2){
                            case 1:
                                Question question = clientExamUI.addExamQuestion();
                                cic.getOos().writeObject(ApiRoute.ADD_QUESTION);
                                cic.getOos().writeObject(question);
                                System.out.println("客户端发送新增考题信息成功！");
                                QuestionMessage questionMessage = (QuestionMessage) cic.getOis().readObject();
                                if("success".equals(questionMessage.getMessage())){
                                    System.out.println("添加考题信息成功！");
                                }
                                else{
                                    System.out.println("添加考题信息失败！");
                                }
                                break;
                            case 2:
                                Question question1 = clientExamUI.deleteExamQuestion();
                                cic.getOos().writeObject(ApiRoute.DELETE_QUESTION);
                                cic.getOos().writeObject(question1);
                                System.out.println("客户端发送删除考题信息成功！");
                                QuestionMessage questionMessage1 = (QuestionMessage) cic.getOis().readObject();
                                if("success".equals(questionMessage1.getMessage())){
                                    System.out.println("删除考题信息成功！");
                                }
                                else{
                                    System.out.println("删除考题信息失败！");
                                }
                                break;
                            case 3:
                                QuestionUpdVo questionUpdVo = clientExamUI.updateQuestion();
                                cic.getOos().writeObject(ApiRoute.UPDATE_QUESTION);
                                cic.getOos().writeObject(questionUpdVo);
                                System.out.println("客户端发送修改考题信息成功！");
                                QuestionUpdVo questionUpdVo1 = (QuestionUpdVo) cic.getOis().readObject();
                                if("success".equals(questionUpdVo1.getQuestionMessage().getMessage())){
                                    System.out.println("修改考题信息成功！");
                                }
                                else{
                                    System.out.println("修改考题信息失败！");
                                }
                                break;
                            case 4:
                                Question question2 = clientExamUI.selectQuestion();
                                cic.getOos().writeObject(ApiRoute.SELECT_QUESTION);
                                cic.getOos().writeObject(question2);
                                System.out.println("客户端发送查找考题信息成功！");
                                QuestionMessage questionMessage2 = (QuestionMessage) cic.getOis().readObject();
                                if("success".equals(questionMessage2.getMessage())){
                                    System.out.println("查找考题信息成功！");
                                    System.out.println("该考题信息为：" + questionMessage2.getQuestion());
                                }
                                else{
                                    System.out.println("查找考题信息失败！");
                                }
                                break;
                            case 5:
                                try {
                                    List<Question> questions = clientExamUI.importQuestion();
                                    if(questions == null) {
                                        System.out.println("文件不存在！");
                                    }
                                    if(questions == null || questions.size() <= 0){
                                        System.out.println("请确认文件格式是否正确！");
                                    }
                                    else{
                                        // 调用后台校验并保存考题
                                        cic.getOos().writeObject(ApiRoute.IMPORT_QUESTION);
                                        cic.getOos().writeObject(questions);
                                        System.out.println("客户端发送导入考题信息成功！");
                                        QuestionImportVo questionImportVo = (QuestionImportVo) cic.getOis().readObject();
                                        if("success".equals(questionImportVo.getMessage())){
                                            System.out.println("导入考题信息成功！");
                                        }
                                        else{
                                            System.out.println("导入考题信息失败！");
                                        }
                                    }
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                                break;
                            case 6:
                                break clientExamUI;
                            default:
                                System.out.println("输入错误，请重新选择！");
                        }
                    }
                    break;
                case 3:
                    // 退出之前将内容写到文件之中
//                    cic.clientClose();
                    // 客户端退出时发送一个路由，代表告诉服务器端 退出与服务器端的线程连接
                    cic.getOos().writeObject(ApiRoute.EXIT_TRAINEE);
                    return;
                default:
                    System.out.println("输入错误，请重新选择！");
            }
        }
    }
}
